<?php
    class Character extends CI_Controller {
        function __construct()
        {
            parent::__construct();
            $this->data['logged'] = $this->user->is_user_logged_id();
            $this->data['loggedChar'] = $this->user->is_character_logged_id();
        }

        public function index() {
            $this->data['title'] = 'Character';
            $this->data['admin'] = $this->session->userdata('admin') == 1 ? true : false;
            $this->data['page'] = 'character';

            $this->load->view('templates/header', $this->data);
            $this->load->view('templates/links-wo-js');
            $this->load->view('templates/menu');
            $this->load->view('character/index');
            $this->load->view('templates/extra');
            $this->load->view('templates/footer');
        }

        public function profile() {
            $this->data['title'] = 'Character Profile';
            $this->data['admin'] = $this->session->userdata('admin') == 1 ? true : false;
            $this->data['page'] = 'character';
            $this->data['character'] = $this->Character_model->getUserStats($this->session->userdata('charId'));

            $this->load->view('templates/header', $this->data);
            $this->load->view('templates/links-wo-js');
            $this->load->view('templates/menu');
            $this->load->view('character/profile');
            $this->load->view('templates/extra');
            $this->load->view('templates/footer');
        }

        public function add_stats() {
            // TODO add stats method
        }

        public function shop($id) {
            $this->data['title'] = 'Character Profile';
            $this->data['admin'] = $this->session->userdata('admin') == 1 ? true : false;
            $this->data['page'] = 'character';
            $this->data['character'] = $this->Character_model->getUserStats($this->session->userdata('charId'));
            $this->data['characterName'] = $this->data['character']['name'];
            $this->data['shopId'] = $id;

            $shop = $this->Character_model->getShopStats($id);
            $shopItems = $this->Character_model->getShopItems($shop['ID']);

            $this->data['shopName'] = $shop['name'];

            foreach($shopItems as $key => $i) {
                $item = $this->Character_model->getItemStats($i['item_id']);
                $shopItems[$key] = $item;
            }

            $inventory = $this->Character_model->getUserInventory($this->session->userdata('charId'));
            foreach($inventory as $key => $i) {
                $item = $this->Character_model->getItemStats($i['item_id']);
                $inventory[$key]['item_id'] = $item;
            }

            $this->data['shopItems'] = $shopItems;
            $this->data['inventory'] = $inventory;

            $this->load->view('templates/header', $this->data);
            $this->load->view('templates/links-w-js');
            $this->load->view('templates/menu');

            if($this->input->post()) {
                $itemId = $this->input->post('item-id');
                $action = $this->input->post('action');

                switch($action) {
                    case 'buy':
                        $itemStats = $this->Character_model->getItemStats($itemId);
                        $buyValue = $itemStats['buyvalue'];
                        echo $buyValue;
                        break;
                    case 'sell':
                        break;
                }

                $this->load->view('character/shop');
            } else {
                $this->load->view('character/shop');
            }
            $this->load->view('templates/extra');
            $this->load->view('templates/footer');
        }

        public function bank() {
            // TODO bank method
        }

        public function equipment() {
            // TODO equipment method
        }

        public function explore() {
            // TODO explore method
        }

        public function logout() {
            $this->session->unset_userdata('charId');
            redirect('account/');
        }

        public function temple() {
            // TODO temple method
        }

        public function town() {
            $this->data['title'] = 'Character Profile';
            $this->data['admin'] = $this->session->userdata('admin') == 1 ? true : false;
            $this->data['page'] = 'character';

            $character = $this->Character_model->getUserStats($this->session->userdata('charId'));

            $this->data['characterName'] = $character['name'];
            $this->data['shops'] = $this->Character_model->getAllShops();

            $zone = $this->Character_model->getZoneStats($character['zone_id']);

            $this->data['zone'] = $zone['name'];

            $this->load->view('templates/header', $this->data);
            $this->load->view('templates/links-wo-js');
            $this->load->view('templates/menu');
            $this->load->view('character/town');
            $this->load->view('templates/extra');
            $this->load->view('templates/footer');
        }
    }
?>
